package cn.lf.android.lawfirm.utils;


/**
 * 判断是否有emoj表情
 *
 * @author ly
 */
public class EmojiFilter {

    private static boolean isNotEmojiCharacter(char codePoint) {
        if(codePoint == 0x263A || codePoint == 0x270A || codePoint == 0x270C){
            return false;
        }
        return (codePoint == 0x0) ||
                (codePoint == 0x9) ||
                (codePoint == 0xA) ||
                (codePoint == 0xD) ||
                ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) ||
                ((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) ||
                ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF));
    }

    /**
     * 过滤emoji 或者 其他非文字类型的字符
     *
     * @param source
     * @return
     */
    public static String filterEmoji(String source) {
        int tag = 0;
        int len = source.length();
        StringBuilder buf = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char codePoint = source.charAt(i);
            if (isNotEmojiCharacter(codePoint)) {
                DebugLog.e("=====if====");
                tag++;
                buf.append(codePoint);
            } else {
                if (tag != 9) {
                    buf.append(codePoint);
                }
                DebugLog.e("=====else====");
            }
        }
        return buf.toString();
    }


    /**
     * 过滤emoji 或者 其他非文字类型的字符
     *
     * @param source
     * @return add by YZL 上个方法 tag值会引起过滤失效,不知道为何要使用tag
     */
    public static String filterEmoji2(String source) {
        int len = source.length();
        StringBuilder buf = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char codePoint = source.charAt(i);
            if (isNotEmojiCharacter(codePoint)) {
                buf.append(codePoint);
            }
        }
        return buf.toString();
    }
}
